package com.mengniu.commerce.sync;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mengniu.commerce.common.core.domain.R;
import com.mengniu.commerce.common.core.enums.SourceEnum;
import com.mengniu.commerce.common.template.enums.APIUrlEnum;
import com.mengniu.commerce.sync.api.base.MnResult;
import com.mengniu.commerce.sync.api.domain.MemberModifyModel;
import com.mengniu.commerce.sync.api.domain.MemberRegisterModel;
import com.mengniu.commerce.sync.api.domain.PointsIncreaseModel;
import com.mengniu.commerce.sync.api.domain.PointsReduceModel;
import com.mengniu.commerce.sync.api.dto.RetryDTO;
import com.mengniu.commerce.sync.common.constant.ApiVersionConstants;
import com.mengniu.commerce.sync.common.constant.OperationUnitTypeConstants;
import com.mengniu.commerce.sync.common.utils.RestTemplateUtil;
import com.mengniu.commerce.sync.exception.RetryException;
import com.mengniu.commerce.sync.service.PointsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.retry.RetryContext;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Retryable;
import org.springframework.retry.support.RetrySynchronizationManager;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
public class TestController {


    public static final String baseURL = "https://t-openapi-gateway.mengniu.cn/";

    @Resource
    private RestTemplateUtil restTemplateUtil;
    @Resource
    private RestTemplateUtil MemberService;


    public static final String clientId = "7b420980-9efd-4d4b-8209-809efd4d4b99";
    public static final String secret = "83BA7A00-3025-4A5A-BA7A-0030252A5A03";

    @Resource
    private com.mengniu.commerce.sync.service.MemberService memberService;

    @Resource
    private com.mengniu.commerce.sync.service.ItemService itemService;

    @Resource
    private PointsService pointsService;


    @GetMapping("/test")
    public JSONObject test() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("page", 1);
        jsonObject.put("size", 10);
        jsonObject.put("fromSys", "APP_SFA_XN");
        JSONObject object = restTemplateUtil.doPost(jsonObject, baseURL + APIUrlEnum.ITEM_SEARCH.getPath());
        return object;
    }

    @GetMapping("/test1")
    public R test1() {
        MemberModifyModel memberModifyModel = new MemberModifyModel();
        memberModifyModel.setMobile("18680552524");
        memberModifyModel.setName("张国宁");
        memberModifyModel.setNickName("张国宁");
        JSONObject param = (JSONObject) JSON.toJSON(memberModifyModel);
        RetryDTO retryDTO = new RetryDTO();
        retryDTO.setSource("YZ");
        retryDTO.setStoreName("QY测试店铺");
        R r = memberService.memberModify(memberModifyModel, retryDTO);
        return r;
    }

    @GetMapping("/test2")
    public R test2() {
        MemberRegisterModel memberRegisterModel = new MemberRegisterModel();
        SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateTime = yyyyMMdd.format(new Date());
        memberRegisterModel.setMobile("18680552524");
        memberRegisterModel.setAnniversarySource("QY测试店铺");
        memberRegisterModel.setAnniversaryDate(dateTime);
        memberRegisterModel.setMembershipAgreementTime(dateTime);
        memberRegisterModel.setMembershipAgreementVersion("v1.0.0");
        RetryDTO retryDTO = new RetryDTO();
        retryDTO.setSource("YZ");
        retryDTO.setStoreName("QY测试店铺");
        R r = memberService.memberRegister(memberRegisterModel,retryDTO, "18680552524");
        return r;
    }

    @GetMapping("/test3")
    public JSONObject test3() {
        MemberRegisterModel memberRegisterModel = new MemberRegisterModel();
        SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateTime = yyyyMMdd.format(new Date());
        memberRegisterModel.setOperationUnitType(OperationUnitTypeConstants.MN_QY_MEMBER);
        memberRegisterModel.setApiVersion(ApiVersionConstants.V_2_0);
        memberRegisterModel.setMobile("18680552524");
        memberRegisterModel.setAnniversarySource("QY测试店铺");
        memberRegisterModel.setAnniversaryDate(dateTime);
        memberRegisterModel.setMembershipAgreementTime(dateTime);
        memberRegisterModel.setMembershipAgreementVersion("v1.0.0");

        MemberRegisterModel memberRegisterModel2 = new MemberRegisterModel();
        memberRegisterModel2.setOperationUnitType(OperationUnitTypeConstants.MN_QY_MEMBER);
        memberRegisterModel2.setApiVersion(ApiVersionConstants.V_2_0);
        memberRegisterModel2.setMobile("15982722321");
        memberRegisterModel2.setAnniversarySource("QY测试店铺");
        memberRegisterModel2.setAnniversaryDate(dateTime);
        memberRegisterModel2.setMembershipAgreementTime(dateTime);
        memberRegisterModel2.setMembershipAgreementVersion("v1.0.0");

        String batchNo = SourceEnum.YZ.getSource() + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        List<MemberRegisterModel> memberRegisterModelList = new ArrayList<>();
        memberRegisterModelList.add(memberRegisterModel);
        memberRegisterModelList.add(memberRegisterModel2);
        JSONObject param = new JSONObject();
        param.put("batchNo",batchNo);
        param.put("list",memberRegisterModelList);
        JSONObject result = restTemplateUtil.doPost(param, baseURL + APIUrlEnum.MEMBER_INSERT.getPath());
        return result;
    }

    @GetMapping("/test4")
    @Retryable(value = { RetryException.class }, maxAttempts = 8, backoff = @Backoff(delay = 1000))
    public JSONObject test4(@RequestParam Integer age)  {
        return this.test4Go(age);
    }

    public JSONObject test4Go(Integer age){
        RetryContext context = RetrySynchronizationManager.getContext();
        System.out.println(context);
        try {
            System.out.println(age);
            int a = age/0;
        }catch (Exception e){
            System.out.println("重试了");
            throw new RetryException("发生了重试异常", e);
        }
        return null;
    }

    @GetMapping("/test5")
    public R test5() throws RetryException {
        PointsIncreaseModel pointsIncreaseModel = new PointsIncreaseModel();
        pointsIncreaseModel.setOperationUnitType(OperationUnitTypeConstants.MN_QY_MEMBER);
        pointsIncreaseModel.setMobile("15912579391");
        pointsIncreaseModel.setPointsTemplateId(1);
        pointsIncreaseModel.setPointsValue(10);
        pointsIncreaseModel.setChangeReasonCode("APP_DSZJJ");
        pointsIncreaseModel.setExpireTime(System.currentTimeMillis() + 60 * 60 * 60 * 1000);
        pointsIncreaseModel.setChannelName("QY测试店铺");
        RetryDTO retryDTO = new RetryDTO();
        return  pointsService.pointsIncrease(pointsIncreaseModel,retryDTO);
    }


    @GetMapping("/test6")
    public R test6() throws RetryException {
        PointsReduceModel pointsReduceModel = new PointsReduceModel();
        pointsReduceModel.setOperationUnitType(OperationUnitTypeConstants.MN_QY_MEMBER);
        pointsReduceModel.setMobile("15912579391");
        pointsReduceModel.setPointsTemplateId(1);
        pointsReduceModel.setPointsValue(10);
        pointsReduceModel.setChangeReasonCode("减少积分");
        pointsReduceModel.setChannelName("QY测试店铺");
        RetryDTO retryDTO = new RetryDTO();
        return pointsService.pointsReduce(pointsReduceModel,retryDTO);
    }
}

